package com.wcpdoc.survey.report.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wcpdoc.survey.core.controller.BaseController;
import com.wcpdoc.survey.core.entity.Chart;
import com.wcpdoc.survey.core.entity.PageIn;
import com.wcpdoc.survey.core.entity.PageOut;
import com.wcpdoc.survey.core.entity.PageResult;
import com.wcpdoc.survey.core.entity.PageResultEx;
import com.wcpdoc.survey.report.service.StatisAnlsService;
import com.wcpdoc.survey.survey.entity.PaperQuestionEx;
import com.wcpdoc.survey.survey.entity.Survey;
import com.wcpdoc.survey.sys.cache.DictCache;
import com.wcpdoc.survey.sys.entity.Dict;

/**
 * 统计分析控制层
 * 
 * v1.0 zhanghc 2017年10月28日上午11:38:27
 */
@Controller
@RequestMapping("/statisAnls")
public class StatisAnlsController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(StatisAnlsController.class);
	
	@Resource
	private StatisAnlsService statisAnlsService;
	
	/**
	 * 到达统计分析列表页面 
	 * 
	 * v1.0 zhanghc 2017年10月28日上午11:38:27
	 * @return String
	 */
	@RequestMapping("/toList")
	public String toList() {
		try {
			return "/WEB-INF/jsp/report/statisAnls/statisAnlsList.jsp";
		} catch (Exception e) {
			log.error("到达统计分析列表页面错误：", e);
			return "/WEB-INF/jsp/report/statisAnls/statisAnlsList.jsp";
		}
	}
	
	/**
	 * 获取调查分类树型列表
	 * 
	 * v1.0 zhanghc 2017年10月28日上午11:38:27
	 * @return List<Map<String,Object>>
	 */
	@RequestMapping("/surveyTypeTreeList")
	@ResponseBody
	public List<Map<String, Object>> surveyTypeTreeList() {
		try {
			return statisAnlsService.getSurveyTypeTreeList();
		} catch (Exception e) {
			log.error("获取调查分类树型列表错误：", e);
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	/**
	 * 统计分析列表 
	 * 
	 * v1.0 zhanghc 2017年10月28日上午11:38:27
	 * @return pageOut
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageOut list(PageIn pageIn) {
		try {
			pageIn.setTen("1");
			return statisAnlsService.getSurveyListpage(pageIn);
		} catch (Exception e) {
			log.error("统计分析列表错误：", e);
			return new PageOut();
		}
	}
	
	/**
	 * 到达统计分析页面 
	 * 
	 * v1.0 zhanghc 2017年10月28日上午11:38:27
	 * @return String
	 */
	@RequestMapping("/toStatisAnls")
	public String toStatisAnls(Model model, Integer surveyId) {
		try {
			model.addAttribute("surveyId", surveyId);
			return "/WEB-INF/jsp/report/statisAnls/statisAnlsStatisAnls.jsp";
		} catch (Exception e) {
			log.error("到达统计分析页面错误：", e);
			return "/WEB-INF/jsp/report/statisAnls/statisAnlsStatisAnls.jsp";
		}
	}
	
	/**
	 * 统计分析列表 
	 * 
	 * v1.0 zhanghc 2017年10月28日上午11:38:27
	 * @return pageOut
	 */
	@RequestMapping("/statisAnlsList")
	public String toPaper(Model model, Integer surveyId) {
		try {
			Survey survey = statisAnlsService.getSurvey(surveyId);
			model.addAttribute("survey", survey);
			
			List<PaperQuestionEx> paperQuestionExList = statisAnlsService.getPaperList(survey.getPaperId());
			model.addAttribute("paperQuestionExList", paperQuestionExList);
			
			List<Dict> list = DictCache.getIndexDictlistMap().get("QUESTION_OPTIONS");
			Map<Integer, String> questionDictMap = new HashMap<>();
			for(Dict dict : list){
				questionDictMap.put(Integer.parseInt(dict.getDictKey()), dict.getDictValue());
			}
			model.addAttribute("dictMap", questionDictMap);
			return "/WEB-INF/jsp/report/statisAnls/statisAnlsStatisAnlsChart.jsp";
		} catch (Exception e) {
			log.error("到达统计分析列表错误：", e);
			return "/WEB-INF/jsp/report/statisAnls/statisAnlsStatisAnlsChart.jsp";
		}
	}
	
	/**
	 * 获取图表数据
	 * 
	 * v1.0 zhanghc 2017年10月29日下午1:48:46
	 * @param surveyId
	 * @param questionId
	 * @return Chart
	 */
	@RequestMapping("/chart")
	@ResponseBody
	public PageResult chart(Integer surveyId, Integer questionId) {
		try {
			Chart chart = statisAnlsService.getChart(surveyId, questionId);
			if(chart == null){
				throw new RuntimeException("未知错误");
			}
			
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("chart", chart);
			return new PageResultEx(true, "获取成功", data);
		} catch (Exception e) {
			log.error("获取图表数据错误：", e);
			return new PageResult(false, "获取失败：" + e.getMessage());
		}
	}
}
